<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class OrderRecharge extends Model
{

    use SoftDelete;

    // 表名
    protected $name = 'order_recharge';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        // 'status_text',
    ];

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 创建充值订单
     */
    public function createOrder($data)
    {
        //订单号
        $data['order_no'] = generateOrderNo();
        //年份
        $data['year'] = date("Y");
        //月份
        $data['month'] = date("n");
        //日期
        $data['day'] = date('j');

        $data['pay_status'] = 1;
        $data['pay_time'] = time();
        $data['pay_amount'] = $data['amount'];

        $res = $this->save($data);

        return $res ? $this->id : false;
    }

    /**
     * 支付订单
     */
    public function payOrder($orderInfo)
    {
        $orderInfo = [
            'amount' => $orderInfo['amount'],
            'orderid' => $orderInfo['order_no'],
            'type' => $orderInfo['pay_type'] == 1 ? 'wechat' : 'alipay',
            'title' => "投研币充值",
            'notifyurl' => 'https://' . $_SERVER['HTTP_HOST'] . '/addons/epay/api/notifyx/type/alipay',
            // 'returnurl' => "",
            'method' => "app",
            // 'openid' => "用户的OpenID",
            // 'auth_code' => "验证码"
        ];

        $res = \addons\epay\library\Service::submitOrder($orderInfo);

        return $res;
    }
}
